<?php
/***************************************************************************
 * 
 * Copyright (c) 2010 , Inc. All Rights Reserved
 * $Id$:View.php,2010/05/11 13:45:47 
 * 
 **************************************************************************/
 
 
 
/**
 * @file View.php
 * @author huqingping
 * @date 2010/05/11 13:45:47
 * @version 1.1 
 * @brief 
 *  
 **/
require_once FR_ROOT.'view/IView.php';
require_once FR_ROOT.'view/HelperAbstract.php';

class ZendView implements IView
{
	private $v = array();
	private $app = null;
	private $viewRoot = '';
	private $helperRoot = '';
	static $HelperCache = array();
	private $cacheHandler = null;

	function init($app)
	{
		$this->app = $app;
		$this->viewRoot = PAGE_ROOT;
		$this->helperRoot = PLUGIN_ROOT.'helper/';
	}
	
	function setArray(Array $arr)
	{
		if ($this->v) {
			$this->v = array_merge($this->v,$arr);
		} else {
			$this->v = $arr;
		}
	}
	
	function assign($key,$value)
	{
		$this->v[$key] = $value;
	}

	function clear()
	{
		$this->v = array();
	}

	function build($template)
	{
		if (strpos($template,'..') !== false ||
			strpos($template,'`') !== false ||
			strpos($template,'!') !== false) {
			//这样操作避免开发人员不正确编码，包含这些字符可能带来安全性问题，
			throw new Exception('hapn.notpl not suport "..|`|!"');
		}
		$this->file = $this->viewRoot.'/'.ltrim($template,'/');
		if (is_readable($this->file)) {
			unset($template);
			ob_start();
			include $this->file;
			$data = ob_get_clean();
			if ($this->app->debug) {
				$data .= '<script>if (hapj) hapj.log.var_dump('.json_encode($this->v).');</script>';
				//$data .= '<textarea rows=30 style="width:100%">'.print_r($this->v,true).'</textarea>';
			}
			return $data;
		}
		throw new Exception('hapn.notpl '.$this->file.' no such file or directory');
	}

	function display($template)
	{
		$data = $this->build($template);
		echo $data;
	}

    function escape($var)
    {
        htmlspecialchars($var, ENT_COMPAT, $this->app->encoding);
    }
    
    private function getCacheHandler()
    {
    	if ($this->cacheHandler === null) {
    		$conf = Conf::get('hapn.pageCacheHandler', false);
    		if ($conf) {
    			if (is_array($conf)) {
    				$this->cacheHandler = call_user_func_array($conf[0], $conf[1]);
    			} else {
    				$this->cacheHandler = call_user_func($conf);
    			}
    		} else {
    			$this->cacheHandler = false;
    		}
    	}
    	return $this->cacheHandler;
    }
    
    //引入局部模块
    function partial($template,$args = array(), $cacheExpire = 0, $cacheKey = '')
    {
    	if ($cacheExpire > 0 && $this->getCacheHandler()) {
    		if (!$cacheKey) {
    			$cacheKey = md5($template.(!empty($args) ? json_encode($args) : ''));
    		} else {
    			$cacheKey = 'pcache.'.$cacheKey;
    		}
    		$handler = $this->cacheHandler;
    		$html = $handler->get($cacheKey);
    		if ($html !== null) {
    			return $html;
    		}
    		$html = $this->_render($template, $args);
    		$handler->set($cacheKey, $html, $cacheExpire);
    		return $html;
    	}
    	return $this->_render($template, $args);
    }
    
    private function _render($template, $args)
    {
    	$view = new ZendView();
    	$view->init($this->app);
    	$args = array_merge($this->v,$args);
    	$view->setArray($args);
    	return $view->build($template);
    }
    
    //通过魔术函数，调用helper功能
    function __call($name,$args)
    {
    	if (isset(self::$HelperCache[$name])) {
    		$helper = self::$HelperCache[$name];
    	} else {
    		$helperFile = $this->_getHelper($name);
    		require_once $helperFile;
    		$class = 'ZendView'.ucfirst($name).'Helper';
    		$helper = new $class();
    		$helper->setView($this);
    		
    		self::$HelperCache[$name] = $helper;
    	}
    	return call_user_func_array(array($helper,$name),$args);
    }
    
    //先查找扩展目录，再查找系统目录
    function _getHelper($name)
    {
    	if ($this->helperRoot) {
    		$file = $this->helperRoot.$name.'.php';
    		if (is_readable($file)) {
    			return $file;
    		}
    	}
    	$file = FR_ROOT.'view/helper/'.$name.'.php';
    	if (is_readable($file)) {
    		return $file;
    	}
    	throw new Exception('hapn.zendview.nohelper no found helper '.$name);
    }
}






/* vim: set ts=4 sw=4 sts=4 tw=100 noet: */
